toastLog("请在无障碍中选择本 APP");
auto.waitFor();
// toastLog("请查看本悬浮窗的“说明”！！")
let option = 0;

let th = null;
let script = null;
let deviceWidth = device.width;
let deviceHeight = device.height;
let wx, wy, downTime, windowX, windowY;
//读取手机号
let rootPath = "./";
let content = files.read(rootPath+"config.txt");
function buildId() {
    return device.buildId;
}

let window = floaty.window(
    <vertical>
        <horizontal>
            <button id="start" text="开始" w="30" h="35" bg="#77ffffff" textSize="10sp"/>
            <button id="stop" text="停止" w="30" h="35" bg="#77ffffff" textSize="10sp"/>
            <button id="move" text="移动" w="30" h="35" bg="#77ffffff" textSize="10sp"/>
        </horizontal>
        <horizontal>
            <button id="setting" text="功能" w="45" h="35" bg="#77ffffff" textSize="10sp"/>
            <button id="about" text="手数" w="45" h="35" bg="#77ffffff" textSize="10sp"/>
        </horizontal>
        <button id="exit" text="退出悬浮窗" w="90" h="35" bg="#77ffffff" textSize="10sp"/>
    </vertical>
);

window.setPosition(parseInt(deviceWidth * 0.08), parseInt(deviceHeight * 0.08));
    setInterval(() => {
}, 1000);

//这个函数是对应悬浮窗的移动
window.move.setOnTouchListener(function (view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            wx = event.getRawX();
            wy = event.getRawY();
            windowX = window.getX();
            windowY = window.getY();
            downTime = new Date().getTime();
            return true;
        case event.ACTION_MOVE:
            //如果按下的时间超过xx秒判断为长按，调整悬浮窗位置
            if (new Date().getTime() - downTime > 300) {
                window.setPosition(windowX + (event.getRawX() - wx), windowY + (event.getRawY() - wy));
            }
            return true;
        case event.ACTION_UP:
            //手指弹起时如果偏移很小则判断为点击
            if (Math.abs(event.getRawY() - wy) < 30 && Math.abs(event.getRawX() - wx) < 30) {
                toastLog("长按调整位置")
            }
            return true;
    }
    return true;
});

//这个函数是对应悬浮窗的退出
window.exit.setOnTouchListener(function (view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            wx = event.getRawX();
            wy = event.getRawY();
            windowX = window.getX();
            windowY = window.getY();
            downTime = new Date().getTime();
            files.write( rootPath + "float.txt", "");
            return true;
        case event.ACTION_MOVE:
            //如果按下的时间超过xx秒判断为长按，调整悬浮窗位置
            if (new Date().getTime() - downTime > 1000) {
                toastLog("退出！");
                exit();
            }
            return true;
        case event.ACTION_UP:
            //手指弹起时如果偏移很小则判断为点击
            if (Math.abs(event.getRawY() - wy) < 30 && Math.abs(event.getRawX() - wx) < 30) {
                toastLog("长按退出脚本")
            }
            return true;
    }
    return true;
});

window.setting.click(() => {
    let items = ["双针反转", "多空指针", '趋势顺打'];
    if (th!=null) {
        toastLog("在运行了哟,请勿重复设置!");
    } else{
        dialogs.select("方式", items, function (index) {
            if (index >= 0) {
                toastLog("已选择第:" + items[index]);
                if (index === 0) {
                    option = 0;
                } else if (index === 1) {
                    option = 1;
                }else{
                    option = 2;
                }
            } else {
                toastLog("取消选择");
            }
        });
    }
});

window.start.click(() => {
    let ss = rootPath+"sc/script.js";
    if (script == null) {
        script = require(ss);
    }
    if (script.length < option + 1) {
        toastLog("该功能未开发，请选择其他功能");
        return;
    }
    if (th == null) {
        //判断是否还有挂单，还有挂单无法买入
        const postData = {  mobile: content,buildId:buildId() };
        var url = "https://anauto.millsecond.cn/api/user/orderList";
        r = http.postJson(url, postData);
        var result = r.body.json();
        if(result.code == 200){
            //查看有没有挂单
            if(result.data.total == 0 ){
                th = threads.start(function () {
                    script[option]();
                });
            }else{
                toast("还有挂单，无法开");
                // toast("还有挂单，无法开启脚本");
            }
        }else{
            toast("网络不通");
        }
    } else {
        if (th.isAlive()) {
            toastLog("在运行了哟");
        } else {
            th = threads.start(function () {
                script[option]();
            });
        }
    }
});

function isInputEmpty(input) {
    return input === null || input === undefined || input === "";
}

window.about.click(() => {
    if (th!=null) {
        toastLog("在运行了哟,请勿重复设置!");
    } else{
        if(option ==0 ){
            dialogs.rawInput("双针反转手数").then(function(inst1) {
                if(isInputEmpty(inst1)){
                    toast("输入为空!");
                }else{
                    let filePath = rootPath + "in1.txt";
                    files.write(filePath, "");
                    files.write(filePath, inst1);
                    toast("双针反转手数设置成功!");
                }
            });

        } else if (option ==1) {
            dialogs.rawInput("多空指针手数").then(function(inst2) {
                if(isInputEmpty(inst2)){
                    toast("输入为空!");
                }else{
                    let filePath = rootPath + "in2.txt";
                    files.write(filePath, "");
                    files.write(filePath, inst2);
                    toast("多空指针手数设置成功!");
                }
            });
        }else if (option ==2) {
            dialogs.rawInput("趋势顺打手数").then(function(inst3) {
                if(isInputEmpty(inst3)){
                    toast("输入为空!");
                }else{
                    let filePath = rootPath + "in3.txt";
                    files.write(filePath, "");
                    files.write(filePath, inst3);
                    toast("趋势顺打手数设置成功!");
                }
            });
        }
    }
});

window.stop.click(() => {
    if (th == null) {
        toastLog("没有进行中的脚本");
    } else {
        if (th.isAlive()) {
            threads.shutDownAll();
            th= null
            files.write( rootPath + "float.txt", "");
            toastLog("停止！");
        } else {
            toastLog("没有进行中的脚本");
        }
    }
});